package link.hwx.controller.oa;

import com.alibaba.fastjson.JSONObject;
import link.hwx.entity.oa.WorkflowDetailTableInfoEntity;
import link.hwx.entity.oa.WorkflowRequestTableField;
import link.hwx.entity.oa.WorkflowRequestTableRecord;
import link.hwx.util.HttpManager;
import link.hwx.util.IdentityVerifyUtil;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程提交实例
 * description :
 * author ：JHY
 * date : 2020/6/4
 * version : 1.0
 */
public class RequestSubmitDemo {

    public static void main(String[] args) {
        RequestSubmitDemo submitDemo = new RequestSubmitDemo();
        submitDemo.submitRequest("3467562", "2982");
    }


    public void submitRequest(String requestid, String userid) {
        IdentityVerifyUtil identityVerifyUtil = IdentityVerifyUtil.getInstance();
        String spk = identityVerifyUtil.getSPK();
        String secret = identityVerifyUtil.getSECRET();
        String token = identityVerifyUtil.getToken();
        if (spk == null || secret == null || token == null) {
            return;
        }
        Map<String, String> heads = IdentityVerifyUtil.getHttpHeads(token, userid, spk);
        String url = IdentityVerifyUtil.HOST + "/api/workflow/paService/submitRequest";
        HttpManager http = new HttpManager();

        Map<String, String> params = new HashMap<>();
        params.put("requestId", requestid);
        params.put("detailData", getFormDetailData());
        params.put("remark", "restful接口提交流程测试");

        try {
            http.cookieStore.clear();
            String result = http.postDataSSL(url, params, heads);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 流程提交明细数据处理
     * 涉及到明细 数据删除、修改、新增
     *
     * @return
     */
    private String getFormDetailData() {
        List<WorkflowDetailTableInfoEntity> details = new ArrayList<>();
        //明细信息
        WorkflowDetailTableInfoEntity detail1 = new WorkflowDetailTableInfoEntity();
        detail1.setTableDBName("formtable_main_1356_dt1");
        //删除明细行ID
        detail1.setDeleteKeys("344");

        //明细数据
        List<WorkflowRequestTableRecord> detailRows = new ArrayList<>();
        WorkflowRequestTableRecord row1 = new WorkflowRequestTableRecord();
        //明细行数据
        List<WorkflowRequestTableField> row1Datas = new ArrayList<>();
        //行字段数据
        WorkflowRequestTableField row1field1 = new WorkflowRequestTableField();
        row1field1.setFieldName("dhwb");
        row1field1.setFieldValue("test02");
        row1Datas.add(row1field1);
        WorkflowRequestTableField row1field2 = new WorkflowRequestTableField();
        row1field2.setFieldName("drl");
        row1field2.setFieldValue("2978,2980,2981");
        row1Datas.add(row1field2);
        row1.setRecordOrder(0);
        row1.setWorkflowRequestTableFields(row1Datas);

        //明细行2
        WorkflowRequestTableRecord row2 = new WorkflowRequestTableRecord();
        List<WorkflowRequestTableField> row2Datas = new ArrayList<>();
        //行字段数据
        WorkflowRequestTableField row2field1 = new WorkflowRequestTableField();
        row2field1.setFieldName("dhwb");
        row2field1.setFieldValue("test01-update-338");
        row2Datas.add(row2field1);

        //行字段数据
        WorkflowRequestTableField row2field2 = new WorkflowRequestTableField();
        row2field2.setFieldName("drl");
        row2field2.setFieldValue("2982,2983");
        row2Datas.add(row2field2);
        row2.setRecordOrder(338);
        row2.setWorkflowRequestTableFields(row2Datas);

        //明细行3
        WorkflowRequestTableRecord row3 = new WorkflowRequestTableRecord();
        List<WorkflowRequestTableField> row3Datas = new ArrayList<>();
        //行字段数据
        WorkflowRequestTableField row3field1 = new WorkflowRequestTableField();
        row3field1.setFieldName("dhwb");
        row3field1.setFieldValue("test01-update");
        row3Datas.add(row3field1);
        row3.setRecordOrder(348);
        row3.setWorkflowRequestTableFields(row3Datas);

        //明细行4
        WorkflowRequestTableRecord row4 = new WorkflowRequestTableRecord();
        List<WorkflowRequestTableField> row4Datas = new ArrayList<>();
        //行字段数据
        WorkflowRequestTableField row4field1 = new WorkflowRequestTableField();
        row4field1.setFieldName("dhwb");
        row4field1.setFieldValue("test01-update-339");
        row4Datas.add(row4field1);

        //行字段数据
        WorkflowRequestTableField row4field2 = new WorkflowRequestTableField();
        row4field2.setFieldName("xlk");
        row4field2.setFieldValue("2");
        row4Datas.add(row4field2);
        row4.setRecordOrder(349);
        row4.setWorkflowRequestTableFields(row4Datas);

        detailRows.add(row2);
        detailRows.add(row4);
        detailRows.add(row3);
        detailRows.add(row1);

        detail1.setWorkflowRequestTableRecords(detailRows);
        details.add(detail1);
        return JSONObject.toJSONString(details);
    }

}
